package com.j283_5.wisdom_park.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.j283_5.wisdom_park.common.result.ResultData;
import com.j283_5.wisdom_park.common.utils.FormatTool;
import com.j283_5.wisdom_park.mapper.CarInformationMapper;
import com.j283_5.wisdom_park.mapper.VisIoMapper;
import com.j283_5.wisdom_park.mapper.VisitRecordMapper;
import com.j283_5.wisdom_park.mapper.VisitorsMapper;
import com.j283_5.wisdom_park.pojo.CarInformation;
import com.j283_5.wisdom_park.pojo.VisIo;
import com.j283_5.wisdom_park.pojo.VisitRecord;
import com.j283_5.wisdom_park.pojo.Visitors;
import com.j283_5.wisdom_park.service.VisitorsService;
import com.j283_5.wisdom_park.vo.visitor_management.AddVisitorsVo;
import com.j283_5.wisdom_park.vo.visitor_management.VisIoVo;
import com.j283_5.wisdom_park.vo.visitor_management.VisitorsInfoVo;
import com.j283_5.wisdom_park.vo.visitor_management.VisitorsVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 访客表 服务实现类
 * </p>
 *
 * @author laoda
 * @since 2022-04-28
 */
@Service
@Transactional
public class VisitorsServiceImpl extends ServiceImpl<VisitorsMapper, Visitors> implements VisitorsService {
    @Resource
    private VisitorsMapper visitorsMapper;
    @Resource
    private CarInformationMapper carInformationMapper;
    @Resource
    private VisitRecordMapper visitRecordMapper;
    @Resource
    private VisIoMapper visIoMapper;


    /**
     * 查询全部访客
     * @param page
     * @param visitors
     * @return
     */
    @Override
    public ResultData visitorsList(Page<Visitors> page, Visitors visitors) {
        QueryWrapper<Visitors> visitorsQueryWrapper = new QueryWrapper<>();
        visitorsQueryWrapper.eq("vis_state",0);
        visitorsQueryWrapper.orderByDesc("leave_time");

        if(visitors.getVisName() != null){
            visitorsQueryWrapper.like("vis_name",visitors.getVisName());
        }
        if(visitors.getVisType() != null){
            visitorsQueryWrapper.eq("vis_type",visitors.getVisType());
        }
        if(visitors.getVisPhone() != null){
            visitorsQueryWrapper.like("vis_phone",visitors.getVisPhone());
        }
        Page<Visitors> visitorsPage = visitorsMapper.selectPage(page, visitorsQueryWrapper);
        List<Visitors> visitorsList = visitorsPage.getRecords();
        Long count = visitorsPage.getTotal();
        ArrayList<VisitorsVo> voArrayList = new ArrayList<>();
        if(visitorsList.size() > 0){
            for(Visitors visitor : visitorsList) {
                VisitorsVo visitorsVo = new VisitorsVo(visitor.getVisId(),visitor.getVisName(),visitor.getVisPhone(),visitor.getVisType(),visitor.getLeaveTime());
                CarInformation carInformation = carInformationMapper.selectById(visitor.getCinfId());
                if(carInformation==null){
                    visitorsVo.setHavCar("否");
                }else{
                    visitorsVo.setHavCar("是");
                    visitorsVo.setCarNumber(carInformation.getCinfMark());
                }
                QueryWrapper<VisitRecord> visitRecordQueryWrapper = new QueryWrapper<>();
                visitRecordQueryWrapper.eq("vis_id",visitor.getVisId());
                VisitRecord visitRecord = visitRecordMapper.selectOne(visitRecordQueryWrapper);
                if(visitRecord!=null){
                    visitorsVo.setVireUnit(visitRecord.getVireUnit());
                }
                voArrayList.add(visitorsVo);
            }
            HashMap<Object, Object> map = new HashMap<>();
            map.put("data",voArrayList);
            map.put("count",count);

            return ResultData.ok().data(map);
        }

        return ResultData.error().message("数据查询异常");
    }


    /**
     * 查询访客详细信息
     * @param visId
     * @return
     */
    @Override
    public ResultData findOneVisitors(Integer visId) {
        Visitors visitors = visitorsMapper.selectById(visId);
        if(visitors != null) {
            VisitorsInfoVo visitorsInfoVo = new VisitorsInfoVo();
            BeanUtils.copyProperties(visitors,visitorsInfoVo);


            QueryWrapper<VisitRecord> visitRecordQueryWrapper = new QueryWrapper<>();
            visitRecordQueryWrapper.eq("vis_id", visId);
            VisitRecord visitRecord = visitRecordMapper.selectOne(visitRecordQueryWrapper);
            if(visitRecord != null){
                visitorsInfoVo.setVisitRecord(visitRecord);
            }
            QueryWrapper<VisIo> visIoQueryWrapper = new QueryWrapper<>();
            visIoQueryWrapper.eq("vis_id",visId);
            List<VisIo> visIoList = visIoMapper.selectList(visIoQueryWrapper);
            ArrayList<VisIoVo> visIoVoList = new ArrayList<>();
            if(visIoList.size() > 0){
                for(VisIo visIo:visIoList){
                    VisIoVo visIoVo = new VisIoVo();
                    BeanUtils.copyProperties(visIo,visIoVo);
                    visIoVo.setVisName(visitors.getVisName());
                    visIoVo.setVisPhone(visitors.getVisPhone());
                    visIoVoList.add(visIoVo);
                }
            }
            if(visIoVoList.size() > 0){
                visitorsInfoVo.setVisIoVo(visIoVoList);
            }
            return ResultData.ok().data(visitorsInfoVo);
        }

        return ResultData.error().message("查询失败");
    }

    /**
     * 添加访客
     * @param addVisitorsVo
     * @return
     */
    @Override
    public ResultData addVisitors(AddVisitorsVo addVisitorsVo) {
        Visitors visitors = new Visitors();
        BeanUtils.copyProperties(addVisitorsVo,visitors);
        Date vireStarttime = FormatTool.getDate(addVisitorsVo.getVireStarttime(), "yyyy-MM-dd");
        Date vireEndtime = FormatTool.getDate(addVisitorsVo.getVireEndtime(), "yyyy-MM-dd");
        visitors.setEntryTime(new Timestamp(vireStarttime.getTime()));
        visitors.setLeaveTime(new Timestamp(vireEndtime.getTime()));
        int i = visitorsMapper.insert(visitors);
        VisitRecord visitRecord = new VisitRecord();
        BeanUtils.copyProperties(addVisitorsVo,visitRecord);
        QueryWrapper<Visitors> visitorsQueryWrapper = new QueryWrapper<>();
        if(!addVisitorsVo.getVirePhone().matches("/^(13[0-9]|14[0-9]|15[0-9]|166|17[0-9]|18[0-9]|19[8|9])\\d{8}$/")){
            return ResultData.error().message("添加手机号格式错误，添加失败");
        }
        visitorsQueryWrapper.eq("vis_phone",addVisitorsVo.getVisPhone());
        Visitors visitor = visitorsMapper.selectOne(visitorsQueryWrapper);
        visitRecord.setVisId(visitor.getVisId());
        visitRecord.setVireStarttime(new Timestamp(vireStarttime.getTime()));
        visitRecord.setVireEndtime(new Timestamp(vireEndtime.getTime()));
        int j = visitRecordMapper.insert(visitRecord);
        if(i>0 && j>0){
            return ResultData.ok().message("添加成功！");
        }
        return ResultData.error().message("添加失败!");
    }


    /**
     * 删除访客（假删）
     * @param visIdList
     * @return
     */
    @Override
    public ResultData deleteVisitors(ArrayList<Integer> visIdList) {
        UpdateWrapper<Visitors> visitorsUpdateWrapper = new UpdateWrapper<>();
        visitorsUpdateWrapper.set("vis_state",1);
        visitorsUpdateWrapper.in("vis_id",visIdList);
        int i = visitorsMapper.update(null, visitorsUpdateWrapper);
        if(i > 0){
            return ResultData.ok().message("删除成功!");
        }
        return ResultData.error().message("删除失败！");
    }
}
